Skip to content

docs: add information about how to test widgets using CachedNetworkImage - #1014

Open
juan-vgv wants to merge 1 commit into
Baseflow:mainfrom
juan-vgv:develop
Open

juan-vgv wants to merge 1 commit into
Baseflow:mainfrom
juan-vgv:develop

Conversation

@juan-vgv

Copy link
Copy Markdown

✨ What kind of change does this PR introduce? (Bug fix, feature, docs update...)

Documentation update.

Testing is a fundamental part of software development and currently there are no official guidelines on how to thoroughly test widgets that contain an instance of CachedNetworkImage.

This PR aims to at least add some basic indication on where to start, but it's not exhaustive by any means.

If there're better ways of implementing these tests I'd be more than happy to know and update this PR accordingly. Also happy to write a more thorough dedicated testing guide with the support from someone on your team!

Thanks a bunch.

⤵️ What is the current behavior?

N/A

🆕 What is the new behavior (if this is a feature change)?

Nope

💥 Does this PR introduce a breaking change?

Nope

🐛 Recommendations for testing

N/A

📝 Links to relevant issues/docs

🤔 Checklist before submitting

  • All projects build
  • Follows style guide lines (code style guide)
  • Relevant documentation was updated
  • Rebased onto current develop

@rickdijk

Copy link
Copy Markdown
Collaborator

This repository now uses main as its only branch, so I have retargeted this PR from develop to main. Nothing about the review changes. If GitHub now shows a conflict, a rebase onto main will clear it, and we will follow up on that separately.

@rickdijk
rickdijk changed the base branch from develop to main September 23, 2026 17:50
@rickdijk

rickdijk commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Hi @juan-vgv, since it's useful to display this info on pub.dev as well, it might be better to put this information in the readme of the package it concerns. At least cached_network_image needs this in the readme, but please check if this applies to cached_network_image_web as well.

However, leave it in the main README here as well

Comment thread README.md
// ...
setUp(() {
CachedNetworkImageProvider.defaultCacheManager = _MockCacheManager();
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe good to mention that the mock needs a stub too:

when(() => cacheManager.getFileStream(
      any(),
      key: any(named: 'key'),
      headers: any(named: 'headers'),
      withProgress: any(named: 'withProgress'),
    )).thenAnswer((_) => const Stream.empty());

Comment thread README.md

However, when testing widgets that use it you might run into an error such as this:
```
Warning: At least one test in this suite creates an HttpClient.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I ran it without a mock, the test failed on MissingPluginException from path_provider, and the HttpClient text only showed up as a warning. Quoting the exception would help people who search for it. The default is also only used when the widget gets no cacheManager:, so apps that pass their own need to fake that one instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants